package org.ala.tiktools.generator.mybatis.creator;

import freemarker.template.TemplateException;
import org.ala.tiktools.generator.mybatis.config.ConfigInfo;
import org.ala.tiktools.generator.mybatis.config.info.Generator;
import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.creator.dao.DaoCreator;
import org.ala.tiktools.generator.mybatis.creator.dao.DaoInfo;
import org.ala.tiktools.generator.mybatis.creator.dao_shard.DaoShardCreator;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityCreator;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityInfo;
import org.ala.tiktools.generator.mybatis.creator.entity.fields.EntityFieldsCreator;
import org.ala.tiktools.generator.mybatis.creator.entity.fields.EntityFieldsInfo;
import org.ala.tiktools.generator.mybatis.creator.entity.pagerCountDO.PagerCountCreator;
import org.ala.tiktools.generator.mybatis.creator.entity.pagerCountDO.PagerCountInfo;
import org.ala.tiktools.generator.mybatis.creator.mapper.MapperCreator;
import org.ala.tiktools.generator.mybatis.creator.mapper.MapperInfo;
import org.ala.tiktools.generator.mybatis.creator.mapper_append.MapperAppendCreator;
import org.ala.tiktools.generator.mybatis.creator.mapper_append.MapperAppendInfo;
import org.ala.tiktools.generator.mybatis.creator.mapper_shard.MapperShardCreator;
import org.ala.tiktools.generator.mybatis.creator.order_by.OrderByCreator;
import org.ala.tiktools.generator.mybatis.creator.order_by.OrderByInfo;
import org.ala.tiktools.generator.mybatis.creator.query.QueryCreator;
import org.ala.tiktools.generator.mybatis.creator.query.QueryInfo;
import org.ala.tiktools.generator.mybatis.creator.shard.ShardRouterCreator;
import org.ala.tiktools.generator.mybatis.creator.shard.ShardRouterInfo;
import org.ala.tiktools.generator.mybatis.dbinfo.DBConnector;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;

import java.io.IOException;

/**
 * @author ala
 * @date 2024-12-31 13:46
 */
public class Creator {

    protected EntityCreator entityCreator = new EntityCreator();
    protected EntityFieldsCreator entityFieldsCreator = new EntityFieldsCreator();
    protected PagerCountCreator pagerCountCreator = new PagerCountCreator();
    protected QueryCreator queryCreator = new QueryCreator();
    protected OrderByCreator orderByCreator = new OrderByCreator();
    protected DaoCreator daoCreator = new DaoCreator();
    protected DaoShardCreator daoShardCreator = new DaoShardCreator();
    protected MapperAppendCreator mapperAppendCreator = new MapperAppendCreator();
    protected MapperCreator mapperCreator = new MapperCreator();
    protected MapperShardCreator mapperShardCreator = new MapperShardCreator();
    protected ShardRouterCreator shardHandlerCreator = new ShardRouterCreator();

    /**
     *  根据配置信息生成
     */
    public void create(ConfigInfo configInfo) throws TemplateException, IOException {
        for (Generator generator : configInfo.getGenerators()) {
            createParts(generator);
        }
    }
    protected void createParts(Generator g) throws TemplateException, IOException {
        DBConnector dbConn = DBConnector.build(g.getDbms());
        for (Part part : g.getParts()) {
            createPart(dbConn, part);
        }
    }
    protected void createPart(DBConnector dbConn, Part p) throws TemplateException, IOException {
        for (String table : p.getTables()) {
            TableInfo t = dbConn.tableInfo(p, p.getDatabase(), table, "utf-8");

            //  生成Entity类
            EntityInfo e = entityCreator.create(p, t);
            EntityFieldsInfo f = entityFieldsCreator.create(p, e, t);

            //  生成Query类
            QueryInfo q = queryCreator.create(p, t, e);

            //  生成OrderBy类
            OrderByInfo ob = orderByCreator.create(p, t, e);

            //  生成dao类
            DaoInfo d = null;
            if (t.isShardTable()) {
                //  生成pagerCount类
                PagerCountInfo pc = pagerCountCreator.create(p, t, e);
                //  生成router类
                ShardRouterInfo[] rs = shardHandlerCreator.create(p, t, e, q);
                d = daoShardCreator.create(p, t, e, q, ob, rs, pc, f);
            } else {
                d = daoCreator.create(p, t, e, q, ob, f);
            }

            //  生成mapper.xml
            MapperInfo m = null;
            //  如果是分表mapper
            if (t.isShardTable()) {
                m = mapperShardCreator.create(p, t, e, d);
            } else {
                m = mapperCreator.create(p, t, e, d);
            }

            //  生成mapper-append.xml
            MapperAppendInfo mai = mapperAppendCreator.create(p, t, e, d);
        }
    }
}
